/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Com.Entity;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Ardha
 */
public class Karyawan {

    private String username;
    private String password;
    private String firstName;
    private String lastName;
    private String jabatan;
    private static Karyawan karyawan;
    private Connection konek = null;
    private static Karyawan loginController;

    public Karyawan() {
    }

    public Karyawan(Connection konek) {
        this.konek = konek;
    }

    public static Karyawan getKoneksiKontrol() throws SQLException, FileNotFoundException, IOException {
        if (loginController == null) {
            loginController = new Karyawan(Koneksi.getDBConnection());
        }
        return loginController;
    }

    public static void logout() throws Exception {
        Karyawan kar = new Karyawan();
        kar.setUsername("out");
        kar.setPassword("out");
        Karyawan.setUserID(kar);
        Koneksi.getDBConnection().close();
    }

    public String login(Karyawan kar) throws SQLException {
        String pswd = null;
        String sql = "CALL loginSP(?,?,?)";
        CallableStatement callst = konek.prepareCall(sql);
        callst.setObject(1, kar.getUsername());
        callst.setObject(2, kar.getPassword());
        callst.registerOutParameter(3, java.sql.Types.VARCHAR);
        callst.execute();
        pswd = callst.getString(3);
        callst.close();
        konek.commit();
        return pswd;
    }

    public List<Karyawan> getDataKaryawan(Karyawan karyawan) throws SQLException, Exception {
        PreparedStatement psmt = null;
        ResultSet rset = null;
        konek.setAutoCommit(false);
        String sql = "select * from karyawan where idkaryawan=(?)";
        psmt = konek.prepareStatement(sql);
        psmt.setObject(1, karyawan.getUsername());
        rset = psmt.executeQuery();
        List<Karyawan> kry = new ArrayList<Karyawan>();
        while (rset.next()) {
            Karyawan kar = new Karyawan();
            kar.setUsername(rset.getString(1));
            kar.setPassword(rset.getString(2));
            kar.setFirstName(rset.getString(3));
            kar.setLastName(rset.getString(4));
            kar.setJabatan(rset.getString(5));
            kry.add(kar);
        }
        konek.commit();
        return kry;
    }

    /**
     * @param karyawan the karyawan to set
     */
    public static void setUserID(Karyawan karyawan) {
        Karyawan.karyawan = karyawan;
    }

    /**
     * @return the karyawan
     * nilai untuk mengambil karyawan yang login
     */
    public static Karyawan getusrID() {
        return karyawan;
    }

    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }

    /**
     * @param username the username to set
     */
    public void setUsername(String username) throws Exception {
        if (username == null || username.matches("")) {
            throw new Exception("Username belum diisi");
        } else if (username.toCharArray().length > 10) {
            throw new Exception("Username salah, masukan lebih dari 10");
        } else {
            this.username = username;
        }
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) throws Exception {
        if (password == null || password.matches("")) {
            throw new Exception("Password belum diisi");
        } else if (password.toCharArray().length > 20) {
            throw new Exception("Password salah, masukan lebih dari 20");
        } else {
            this.password = password;
        }
    }

    /**
     * @return the firstName
     */
    public String getFirstName() {
        return firstName;
    }

    /**
     * @param firstName the firstName to set
     */
    public void setFirstName(String firstName) throws Exception {
        if (firstName == null || firstName.matches("")) {
            throw new Exception("First Name kosong");
        } else if (firstName.toCharArray().length > 20) {
            throw new Exception("First Name salah, masukan lebih dari 15");
        } else if (firstName.matches("[ a-zA-z]*")) {
            this.firstName = firstName;
        } else {
            throw new Exception("First Name salah terdapat angka atau symbol");
        }
    }

    /**
     * @return the lastName
     */
    public String getLastName() {
        return lastName;
    }

    /**
     * @param lastName the lastName to set
     */
    public void setLastName(String lastName) throws Exception {
        if (lastName == null || lastName.matches("")) {
            throw new Exception("Last Name kosong");
        } else if (lastName.toCharArray().length > 20) {
            throw new Exception("Last Name salah, masukan lebih dari 15");
        } else if (lastName.matches("[ a-zA-z]*")) {
            this.lastName = lastName;
        } else {
            throw new Exception("Last Name salah terdapat angka atau symbol");
        }
    }

    /**
     * @return the jabatan
     */
    public String getJabatan() {
        return jabatan;
    }

    /**
     * @param jabatan the jabatan to set
     */
    public void setJabatan(String jabatan) throws Exception {
        if (jabatan == null || jabatan.matches("")) {
            throw new Exception("Jabatan kosong");
        } else if (jabatan.toCharArray().length > 20) {
            throw new Exception("Jabatan salah, masukan lebih dari 15");
        } else if (jabatan.matches("[a-zA-z]*")) {
            this.jabatan = jabatan;
        } else {
            throw new Exception("Jabatan salah terdapat angka atau symbol");
        }
    }
}
